<?php


namespace App\Services;


use Illuminate\Support\Facades\DB;

class GameMoneyReissue
{
    public static function gameMoneyReissue($order_no)
    {
        try {
            $ratio = 1;
            $jili = 1;
            $fujia= 1;
            $jifen = 1;
            $zhuangbei = 1;
            $row = DB::table('game_recharge_order')->where('order_no', '=', $order_no)->limit(1)->get();
            foreach ($row as $key=>$value) {
                $info = (array)$value;
                $money =  $info['money'];//金额
                $gameuser  =  $info['game_account']; //游戏账号
                $platform_id = $info["pay_plat_id"]; //平臺id
                $platformInfo = (array)DB::table('pay_platform')->where("id","=",$platform_id)->first(); //平台详情
                $apiKey =   $platformInfo['app_key'];
                $iuserId = $platformInfo['user_id'];
                $timeOut = 10;
                $codeType = $platformInfo['game_id'];
                $param = ['gameid'=>$codeType,"money"=>$money,"gameuser"=>$gameuser,"ratio"=>$ratio,"jili"=>$jili,"fujia"=>$fujia,"jifen"=>$jifen,"zhuangbei"=>$zhuangbei];
                $sign = md5('1.0'.$iuserId.'manualpay'.$apiKey);
                $curlData = ['iversrion'=>'1.0','iuserid'=>$iuserId,'itype'=>'manualpay','idata'=>json_encode($param),'isign'=>$sign];
                $httph = curl_init();
                curl_setopt($httph, CURLOPT_HTTPHEADER, ['Content-type: application/json']);
                curl_setopt($httph, CURLOPT_URL, 'http://api.64pay.com/api.html');
                curl_setopt($httph, CURLOPT_POST, 1); //设置为POST方式
                //curl_setopt($httph, CURLOPT_CUSTOMREQUEST, "POST");
                curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
                curl_setopt($httph, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($httph, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($httph, CURLOPT_POSTFIELDS, json_encode($curlData));
                curl_setopt($httph, CURLOPT_AUTOREFERER, 1);
                curl_setopt($httph, CURLOPT_TIMEOUT, $timeOut);
                //curl_setopt($httph, CURLOPT_HEADER, 0);
                $rst = curl_exec($httph);
                curl_close($httph);
                $result =  json_decode($rst, true);
                // var_dump($result);
                if ($result['resultcode'] == 200){
                    DB::table('game_recharge_order')
                        ->where('id', $info["id"])
                        ->update([
                            'is_reissue' => 1,
                        ]);
                }else{
                    $order = [
                        'pay_plat_id' => $info["pay_plat_id"],
                        'user_id' => $info["user_id"],
                        'unionid' => $info["unionid"],
                        'money' => $info["money"],
                        'req_params' => $info["req_params"],
                        'pay_type' => $info["pay_type"],
                        'order_no' => $info["order_no"],
                        'plat_order_no' => $info["plat_order_no"],
                        'contact' => $info["contact"] ?? '',
                        'pay_status' => 2,
                        'game_account' => $info["game_account"] ?? '',
                        'game_version' => $info["game_version"]  ?? '',
                        'game_server' => $info["game_server"] ?? '',
                        'game_role' => $info["game_role"] ?? '',
                        'create_time' => time(),
                        'update_time' => time(),
                        'site_host' => $_SERVER['HTTP_HOST'] ?? '',
                        'order_type' => 1,
                    ];
                    DB::table('game_recharge_order_error')->insertGetId($order);
                }
            }
//            echo "批量充值成功";
        } catch (\Exception $e) {
            return false;
        }
    }
}

